{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "returning-hollow",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数，输出六个部分的百分比\n",
    "# 读取一张图片，与模板进行最相似匹配\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image as mpimg\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "complicated-cornell",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取单张图片\n",
    "def imgDeal(ii):\n",
    "    # 读取图片，得出二值图像\n",
    "    lena = mpimg.imread('data/train/%s.jpg'%(ii)) #40,40,3\n",
    "    theShape = lena.shape\n",
    "    lena2 = np.sum(lena,axis=2,keepdims=True)/3\n",
    "    for i in range(theShape[0]):\n",
    "        for j in range(theShape[1]):\n",
    "            if lena2[i,j,0]>200:\n",
    "                lena2[i,j,0]=0\n",
    "            else:\n",
    "                lena2[i,j,0] = 1\n",
    "\n",
    "    # 寻找出角点坐标\n",
    "    tempRow = np.zeros(theShape[0])\n",
    "    tempCollom= np.zeros(theShape[1])\n",
    "    for i in range(theShape[0]):\n",
    "        for j in range(theShape[1]):\n",
    "            if lena2[i,j,0]==1:\n",
    "                tempRow[i] = tempRow[i]+1\n",
    "                tempCollom[j] = tempCollom[j]+1\n",
    "    for i in range(len(tempRow)):\n",
    "        if tempRow[i]!= 0:\n",
    "            xStart =i\n",
    "            for j in range(i,len(tempRow)):\n",
    "                if tempRow[j]== 0:\n",
    "                    xEnd = j\n",
    "                    break\n",
    "            break\n",
    "    xStart=xStart-1\n",
    "    xEnd=xEnd+1\n",
    "    for i in range(len(tempCollom)):\n",
    "        if tempCollom[i]!= 0:\n",
    "            yStart =i\n",
    "            for j in range(i,len(tempCollom)):\n",
    "                if tempCollom[j]== 0:\n",
    "                    yEnd = j\n",
    "                    break\n",
    "            break    \n",
    "    yStart = yStart-1\n",
    "    yEnd = yEnd+1\n",
    "\n",
    "    # 分成六块，计算百分比\n",
    "    tempX = (xEnd-xStart)//3\n",
    "    tempY = (yEnd-yStart)//2\n",
    "    result = []\n",
    "\n",
    "    t_1 = 0\n",
    "    t_0 = 0\n",
    "    for i in range(xStart,xStart+tempX):\n",
    "        for j in range(yStart,yStart+tempY):\n",
    "            if lena2[i,j,0] == 0:\n",
    "                t_0 = t_0 + 1\n",
    "            if lena2[i,j,0] == 1:\n",
    "                t_1 = t_1 +1\n",
    "    result.append(t_1/(t_1+t_0))\n",
    "\n",
    "    t_1 = 0\n",
    "    t_0 = 0\n",
    "    for i in range(xStart+tempX,xStart+tempX*2):\n",
    "        for j in range(yStart,yStart+tempY):\n",
    "            if lena2[i,j,0] == 0:\n",
    "                t_0 = t_0 + 1\n",
    "            if lena2[i,j,0] == 1:\n",
    "                t_1 = t_1 +1\n",
    "    result.append(t_1/(t_1+t_0))\n",
    "\n",
    "    t_1 = 0\n",
    "    t_0 = 0\n",
    "    for i in range(xStart+tempX*2,xStart+tempX*3):\n",
    "        for j in range(yStart,yStart+tempY):\n",
    "            if lena2[i,j,0] == 0:\n",
    "                t_0 = t_0 + 1\n",
    "            if lena2[i,j,0] == 1:\n",
    "                t_1 = t_1 +1\n",
    "    result.append(t_1/(t_1+t_0))\n",
    "\n",
    "    t_1 = 0\n",
    "    t_0 = 0\n",
    "    for i in range(xStart,xStart+tempX):\n",
    "        for j in range(yStart+tempY,yStart+tempY*2):\n",
    "            if lena2[i,j,0] == 0:\n",
    "                t_0 = t_0 + 1\n",
    "            if lena2[i,j,0] == 1:\n",
    "                t_1 = t_1 +1\n",
    "    result.append(t_1/(t_1+t_0))\n",
    "\n",
    "    t_1 = 0\n",
    "    t_0 = 0\n",
    "    for i in range(xStart+tempX,xStart+tempX*2):\n",
    "        for j in range(yStart+tempY,yStart+tempY*2):\n",
    "            if lena2[i,j,0] == 0:\n",
    "                t_0 = t_0 + 1\n",
    "            if lena2[i,j,0] == 1:\n",
    "                t_1 = t_1 +1\n",
    "    result.append(t_1/(t_1+t_0))\n",
    "    t_1 = 0\n",
    "    t_0 = 0\n",
    "    for i in range(xStart+tempX*2,xStart+tempX*3):\n",
    "        for j in range(yStart+tempY,yStart+tempY*2):\n",
    "            if lena2[i,j,0] == 0:\n",
    "                t_0 = t_0 + 1\n",
    "            if lena2[i,j,0] == 1:\n",
    "                t_1 = t_1 +1\n",
    "    result.append(t_1/(t_1+t_0))\n",
    "#     print(result)\n",
    "    return result\n",
    "#     for i in range(xStart,xEnd):\n",
    "#         for j in range(yStart,yEnd):\n",
    "#             print(int(lena2[i,j,0]),end=' ')\n",
    "#         print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "inclusive-fabric",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: [0.275, 0.05, 0.0, 0.65, 0.6, 0.6], 2: [0.390625, 0.0, 0.4375, 0.53125, 0.484375, 0.390625], 3: [0.3055555555555556, 0.041666666666666664, 0.3333333333333333, 0.5138888888888888, 0.5555555555555556, 0.4444444444444444], 4: [0.05, 0.3375, 0.375, 0.375, 0.3125, 0.525], 5: [0.37037037037037035, 0.32098765432098764, 0.3333333333333333, 0.25925925925925924, 0.4567901234567901, 0.43209876543209874], 6: [0.19753086419753085, 0.49382716049382713, 0.4074074074074074, 0.18518518518518517, 0.41975308641975306, 0.41975308641975306], 7: [0.3194444444444444, 0.041666666666666664, 0.3194444444444444, 0.5138888888888888, 0.3333333333333333, 0.06944444444444445], 8: [0.3888888888888889, 0.5277777777777778, 0.4305555555555556, 0.5138888888888888, 0.5555555555555556, 0.4722222222222222], 9: [0.4074074074074074, 0.37037037037037035, 0.16049382716049382, 0.4567901234567901, 0.5185185185185185, 0.19753086419753085]}\n"
     ]
    }
   ],
   "source": [
    "dict_precent = {}\n",
    "for ii in range(1,10):\n",
    "    result = imgDeal(ii)\n",
    "    dict_precent[ii]=result\n",
    "\n",
    "print(dict_precent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "beautiful-nudist",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.39870689655172414, 0.0, 0.0, 0.8168103448275862, 0.75, 0.7241379310344828]\n"
     ]
    }
   ],
   "source": [
    "result = imgDeal(11)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "proved-finance",
   "metadata": {},
   "outputs": [],
   "source": [
    "def knn(result1,result2):\n",
    "    temp = 0\n",
    "    for i in range(len(result2)):\n",
    "        temp = (result1[i]-result2[i])**2+temp\n",
    "    return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "secure-revolution",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8023862960760999\n"
     ]
    }
   ],
   "source": [
    "print(knn(result,dict_precent[4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "serious-jesus",
   "metadata": {},
   "outputs": [],
   "source": [
    "def judge(jResult):\n",
    "    theMin = -1\n",
    "    theIndex = 0\n",
    "    for i in range(1,10):\n",
    "        temp = knn(jResult,dic_precent[i])\n",
    "        if temp > theMin:\n",
    "            theMin = temp\n",
    "            theIndex = i\n",
    "    ret"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
